package com.zzxy.spring5.factory;

import com.zzxy.spring5.aspct.MyAspect;
import com.zzxy.spring5.service.impl.IStudentService;
import com.zzxy.spring5.service.impl.StudentServiceImpl;
import javassist.util.proxy.ProxyFactory;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyBeanFactory {
    public static IStudentService creatStudentService() {
        StudentServiceImpl studentService = new StudentServiceImpl();
        MyAspect aspect = new MyAspect();
        IStudentService stuServicPoxy = (IStudentService) Proxy.newProxyInstance(MyBeanFactory.class.getClassLoader(), studentService.getClass().getInterfaces(), new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                aspect.before();
                Object obj= method.invoke(studentService, args);
                aspect.after();

                return obj;
            }
        });
        return stuServicPoxy;
    }
}
